<?php

class newsC {

	public function __construct(){
		FunctionsHelper::includeFiles(
			array(
				PUBLIC_MODELS_DIRECTORY . 'model.php',
				PUBLIC_MODELS_DIRECTORY . 'news.php',
				PUBLIC_MODELS_DIRECTORY . 'user.php',
				PUBLIC_MODELS_DIRECTORY . 'comment.php'
			)
		);
	}
	
	public function getAll(){
		$arrNews = array();

		FunctionsHelper::includeFiles(
			array(
				ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
			)
		);
		
		$News = new News();
		$arrResult = $News->getAllRSS();
		foreach($arrResult as $news){
			$users = new User(array('user_id' => $news['user_id']));
			$user = $users->getUser();
			$news['user_id'] = $user;
			$arrNews[] = new News($news);
		}

		return $arrNews;
	}

	public function getTopNews(){
		$News = new News();
		$News->setOrderBy("created");
		$News->setType("news");
		$TopNews = $News->getCountAll(AMOUNT_TOP_NEWS);

		return $this->_createNewsObject($TopNews);
	}
	
	public function getOlderNews(){
		$News = new News();
		$News->setOrderBy("created");
		$News->setType("news");
		$TopNews = $News->getCountAll(AMOUNT_NEWS_AT_MAIN, AMOUNT_TOP_NEWS);

		return $this->_createNewsObject($TopNews);
	}

	public function getOne($arrDescription){
		$news = new News($arrDescription);

		$newsArray = $news->getOne();
		$newsArray = $newsArray[0];

		$Creator = $this->_obtainCreator($newsArray["user_id"]);

		$news = new News($newsArray);
		$news->setUser($Creator);
		$news->getResources();
		
		$Images = $news->getImage();
		$Audios = $news->getAudio();
		$Videos = $news->getVideo();
		
		$final_images = array();
		for ($i = 0; $i < count($Images); $i++){
			if (!empty($Images[$i]["tag"])){
				$news->setText(str_replace($Images[$i]["tag"], "<div style='width:100%; text-align: center;'><img src='" . MAIN_IMAGES_NEWS_PATH . $Images[$i]["name_file"] . "' alt='" . $Images[$i]["text"] . "' title='" . $Images[$i]["text"] . "' style='max-width:600px; max-height:auto;' /></div>", $news->getText()));
			} else {
				array_push($final_images, $Images[$i]);
			}
		}
		$news->setImage($final_images);

		$final_audios = array();
		for ($i = 0; $i < count($Audios); $i++){
			if (!empty($Audios[$i]["tag"])){
				$news->setText(str_replace($Audios[$i]["tag"], '<div style="width:100%; text-align: center;"><embed src="' . MAIN_FILES_PATH . $Audios[$i]["name_file"] . '" volume="50" height="60" width="420" autostart="false" loop="false" style="height: 60px;" /></div>', $news->getText()));
			} else {
				array_push($final_audios, $Audios[$i]);
			}
		}
		$news->setAudio($final_audios);
		
		$final_videos = array();
		for ($i = 0; $i < count($Videos); $i++){
			if (!empty($Videos[$i]["tag"])){
				$news->setText(str_replace($Videos[$i]["tag"], '<div style="width:100%; text-align: center;"><iframe class="videoPosition" src="http://www.youtube.com/embed/' . $Videos[$i]["link"] . '" frameborder="0" allowfullscreen></iframe></div>', $news->getText()));
			} else {
				array_push($final_videos, $Videos[$i]);
			}
		}
		$news->setVideo($final_videos);
		
		$news->getTheComments(0, COMMENTS_PER_PAGE);

		return $news;
	}

	private function _obtainCreator($id){
		$Creator = new User(array("id" => $id));
		$Creator = $Creator->getOne();
		return new User($Creator[0]);
	}

	private function _createNewsObject($arrNews){
		$arrNewsObj = array();
		
		foreach ($arrNews as &$news){
			$news['user'] = array ("id" => $news['user_id']);
			$newsObj = new News($news);
			$newsObj->setStringType($news['is_news']);
			$user = $newsObj->getUser()->getOne();
			$newsObj->setUser(new user($user[0]));
			$newsObj->getResources();

			$arrNewsObj[] = $newsObj;
		}
		
		return $arrNewsObj;
	}

	public function GetAllWithTags($tags, $amount = 0){
		$arrTags = FunctionsHelper::createArrayFromText($tags, ",");
		$news = new News();
		$NewsWithTag = $news->getWithTags($arrTags);

		return $this->_createNewsObject($NewsWithTag);
	}

	public function GetAllOf($type, $amount = 0){
		$arrNewsObj = array();
		$news = new news(array('type' => $type));

		if ($amount)
			$arrNews = $news->getCount($amount);
		else
			$arrNews = $news->getAll();

		return $this->_createNewsObject($arrNews);
	}

	public function GetOneImageOf($News){
		$Image = $News->SelectOneImage();
		$News->setImage($Image);
	}
	
	public function PublishComment($arrInitialValues){
		date_default_timezone_set("America/Argentina/Buenos_Aires");
		$news = new News(
			array (
				"id" => $arrInitialValues["news"]
			)
		);
		
		$comment = new comment(
			array (
				"writter_name" => $arrInitialValues["txtName"],
				"email" => $arrInitialValues["txtEmail"],
				"comment" => $arrInitialValues["txtComment"]
			)
		);
		
		$news->setComments($comment);
		return $news->publishComment($comment);
	}

}

?>